package DAO;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class DAO {

	private static EntityManagerFactory emf = null;
	private static EntityManager em = null;
	private static EntityTransaction tx = null ;
	
	public static EntityManager getManager() {
		if (em == null) {
		emf = Persistence.createEntityManagerFactory("projet");
		em = emf.createEntityManager();
		tx = em.getTransaction();
		
		}
		if (!tx.isActive()) {
			tx.begin();
			}
			
		return em;
	}
	
	
	public static void commit() {
	 	em.flush();
	 	//em.clear();
	 	tx.commit();
	}
	
	public static void rollback() {
	 	tx.rollback();
	}
	
	public static void close() {
	 	em.close(); em = null;
	 	emf.close();emf = null;
	}
	
	public static void viderBase() {
		/*getManager().createQuery("DELETE FROM Auteur").executeUpdate();
		getManager().createNativeQuery("ALTER TABLE Auteur AUTO_INCREMENT = 1").executeUpdate();
		getManager().createQuery("DELETE FROM Article").executeUpdate();
		getManager().createNativeQuery("ALTER TABLE Article AUTO_INCREMENT = 1").executeUpdate();
		/*getManager().createQuery("DELETE FROM Page").executeUpdate();
		getManager().createNativeQuery("ALTER TABLE Page AUTO_INCREMENT = 1").executeUpdate();		
		getManager().createQuery("DELETE FROM Genre").executeUpdate();
		getManager().createNativeQuery("ALTER TABLE genre AUTO_INCREMENT = 1").executeUpdate();
		getManager().createQuery("DELETE FROM Concert").executeUpdate();
		getManager().createNativeQuery("ALTER TABLE concert AUTO_INCREMENT = 1").executeUpdate();*/
		}
}
